package org.oimwrapper.connectors.common;

import java.util.*;

import org.oimwrapper.connectors.*;

/**
 * Clears all revoked resource object instances on the system.<p>
 *
 * This task does not expect any parameters in addition to the standard
 * parameters in the {@link OIMTaskBase} class.
 */
public class OIMDeleteRevokedInstances extends OIMTaskBase {

	/**
	 * True if a stop has been requested for this task.
	 */
	private boolean stop;

	/**
	 * Default constructor.
	 */
	public OIMDeleteRevokedInstances() {
		super("OIMWRAPPER.TASK");
		stop = false;
	}

	/**
	 * This method is called by the scheduler to execute the task instance.
	 */
	public void execute() {
		try {
			logger.warn("Clearing all child table entries for revoked object instances...");
			if (!stop) {
				Map[] results = getOimData("select sdk_name from sdk where sdk_type='P' and sdk_key in (select sdh_child_key from sdh)");
				for (int i = 0; i < results.length; i++) {
					String table = (String) results[i].get("Structure Utility.Table Name");
					logger.info("Clearing " + table + "...");
					executeOimQuery("delete from " + table + " where orc_key in (select orc_key from orc where orc_status='X')");
					logger.info("Deleted records from " + table);
				}
			}
			if (!stop) {
				Map[] results = getOimData("select sdk_name from sdk where sdk_type='O' and sdk_key in (select sdh_child_key from sdh)");
				for (int i = 0; i < results.length; i++) {
					String table = (String) results[i].get("Structure Utility.Table Name");
					logger.info("Clearing " + table + "...");
					executeOimQuery("delete from " + table + " where obi_key in (select obi_key from obi where orc_key in (select orc_key from orc where orc_status='X'))");
					logger.info("Deleted records from " + table);
				}
			}
			logger.warn("Clearing all parent table entries for revoked object instances...");
			if (!stop) {
				Map[] results = getOimData("select sdk_name from sdk where sdk_type='P' and sdk_key not in (select sdh_child_key from sdh)");
				for (int i = 0; i < results.length; i++) {
					String table = (String) results[i].get("Structure Utility.Table Name");
					logger.info("Clearing " + table + "...");
					executeOimQuery("delete from " + table + " where orc_key in (select orc_key from orc where orc_status='X')");
					logger.info("Deleted records from " + table);
				}
			}
			if (!stop) {
				Map[] results = getOimData("select sdk_name from sdk where sdk_type='O' and sdk_key not in (select sdh_child_key from sdh)");
				for (int i = 0; i < results.length; i++) {
					String table = (String) results[i].get("Structure Utility.Table Name");
					logger.info("Clearing " + table + "...");
					executeOimQuery("delete from " + table + " where obi_key in (select obi_key from obi where orc_key in (select orc_key from orc where orc_status='X'))");
					logger.info("Deleted records from " + table);
				}
			}
			if (!stop) {
				logger.info("Clearing OSI...");
				executeOimQuery("delete from osi where orc_key in (select orc_key from orc where orc_status='X')");
				logger.info("Deleted records from OSI");
			}
			if (!stop) {
				logger.info("Clearing OSH...");
				executeOimQuery("delete from osh where sch_key not in (select sch_key from osi)");
				logger.info("Deleted records from OSH");
			}
			if (!stop) {
				logger.info("Clearing SCH...");
				executeOimQuery("delete from sch where sch_key not in (select sch_key from osi)");
				logger.info("Deleted records from SCH");
			}
			if (!stop) {
				logger.info("Clearing OBI...");
				executeOimQuery("delete from obi where orc_key in (select orc_key from orc where orc_status='X')");
				logger.info("Deleted records from OBI");
			}
			if (!stop) {
				logger.info("Clearing OIO...");
				executeOimQuery("delete from oio where orc_key in (select orc_key from orc where orc_status='X')");
				logger.info("Deleted records from OIO");
			}
			if (!stop) {
				logger.info("Clearing ATD...");
				executeOimQuery("delete from atd where oiu_key in (select oiu_key from oiu where orc_key in (select orc_key from orc where orc_status='X'))");
				logger.info("Deleted records from OIU");
			}
			if (!stop) {
				logger.info("Clearing OIU...");
				executeOimQuery("delete from oiu where orc_key in (select orc_key from orc where orc_status='X')");
				logger.info("Deleted records from OIU");
			}
			if (!stop) {
				logger.info("Unlinking Recon Events...");
				executeOimQuery("update rpc set orc_key=null where orc_key in (select orc_key from orc where orc_status='X')");
				executeOimQuery("update rcp set orc_key=null where orc_key in (select orc_key from orc where orc_status='X')");
				executeOimQuery("update rce set orc_key=null where orc_key in (select orc_key from orc where orc_status='X')");
				logger.info("Unlinked Recon Events");
			}
			if (!stop) {
				logger.info("Clearing ORC...");
				executeOimQuery("update orc set orc_parent_key=null where orc_status='X'");
				executeOimQuery("update orc set orc_parent_key=null where orc_parent_key in (select orc_key from orc where orc_status='X')");
				executeOimQuery("delete from orc where orc_status='X'");
				logger.info("Deleted records from ORC");
			}
			logger.warn("All revoked object instances have been cleared.");
		} catch (Exception e) {
			OIMExceptionHandler.handleException(logger, e, getClass());
		}
	}

	/**
	 * This method is called by the scheduler to stop a running task instance.
	 * 
	 * @return <code>true</code>
	 */
	public boolean stop() {
		logger.warn(" ---- Stopping current task ---- ");
		stop = true;
		return stop;
	}
}